﻿Imports FinanceiroDAL
Public Class CaixaBLL
    Dim objDAL As New CaixaDAL
#Region "Variáveis de Instância"
    Property caicodigo As Integer
    Property caidescricao As String
    Property dataAbertura As Date
    Property dataFechamento As Nullable(Of Date)
    Property valorInicial As Decimal
    Property valorCaixa As Decimal
    Property Fechamento As Decimal
    Property usuario As Integer
    Property status As Integer
    Property res As Double
#End Region
#Region "Construtor"
    Public Sub New()
    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codig As Integer, descr As String, dtabertura As Date, dtfechamento As Nullable(Of Date), vlrinicial As Decimal, vlrcaixa As Decimal, ByVal fech As Decimal, usu As Integer, stat As Integer, ByVal resul As Double)
        Me.caicodigo = codig
        Me.caidescricao = descr
        Me.dataAbertura = dtabertura
        Me.dataFechamento = dtfechamento
        Me.valorInicial = vlrinicial
        Me.valorCaixa = vlrcaixa 'do dia
        Me.Fechamento = fech
        Me.usuario = usu
        Me.status = stat
        Me.res = resul
    End Sub
#End Region
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New CaixaDAL
            tab = objDAL.localizar(codigo) 'alteração
            If (tab.Rows.Count > 0) Then
                Me.caicodigo = Integer.Parse(tab.Rows(0)("cai_codigo").ToString)
                Me.caidescricao = tab.Rows(0)("cai_descricao").ToString
                Me.dataAbertura = tab.Rows(0)("cai_dtabertura").ToString
                If (tab.Rows(0)("cai_dtfechamento") Is DBNull.Value) Then
                    Me.dataFechamento = Nothing
                Else
                    Me.dataFechamento = tab.Rows(0)("cai_dtfechamento").ToString
                End If
                Me.valorInicial = Decimal.Parse(tab.Rows(0)("cai_valorinicial").ToString)
                Me.valorCaixa = Decimal.Parse(tab.Rows(0)("cai_valorfinal").ToString)
                Me.Fechamento = Decimal.Parse(tab.Rows(0)("cai_fechamento").ToString)
                Me.usuario = Integer.Parse(tab.Rows(0)("usu_codigo").ToString)
                Me.status = Integer.Parse(tab.Rows(0)("sta_codigo").ToString)
                If Me.res <> 0 Then
                    Me.res = Double.Parse(tab.Rows(0)("cai_resultado").ToString)
                Else
                    Me.res = Nothing
                End If

            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region
#Region "Limpar"
    Sub Limpar()
        Me.caicodigo = Nothing
        Me.dataAbertura = Nothing
        Me.dataFechamento = Nothing
        Me.caidescricao = Nothing
        Me.valorInicial = Nothing
        Me.valorCaixa = Nothing
        Me.Fechamento = Nothing
        Me.usuario = Nothing
        Me.status = Nothing
        Me.res = Nothing
    End Sub
#End Region
#Region "Buscar CBFiltro"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New CaixaDAL
            ret = objDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "BUSCAR DO DATAGRID"
    Function buscar() As DataTable 'localizar do datagrid
        Dim tab As DataTable
        Try
            objDAL = New CaixaDAL
            tab = objDAL.carregadg()
        Catch ex As Exception
            Throw New Exception("Dados não encontrados!")
        Finally
            objDAL = Nothing
        End Try
        Return tab
    End Function

    Function locdata(ByVal dataIni As DateTime, ByRef datafim As DateTime) As DataTable 'buscar datas cbfiltro
        Dim ret As DataTable
        Try
            objDAL = New CaixaDAL
            If dataIni <= datafim Then
                ret = objDAL.locadata(dataIni, datafim)
            Else : Throw New Exception("Data inical maior que data final")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "INSERIR"
    Function inserir() As Boolean  'ínsere caixa
        If (Me.status = 4) Then
            Dim ret As Boolean
            Try
                objDAL = New CaixaDAL
                Me.caicodigo = objDAL.inserir(Me.caidescricao, Me.dataAbertura, Me.dataFechamento, Me.valorInicial, Me.valorCaixa, Me.Fechamento, Me.usuario, Me.status, Me.res)
                ' insereItensCaixa()
                ret = True
            Catch ex As Exception
                Throw ex
                ret = False
            Finally
                objDAL = Nothing
            End Try
            Return ret
        Else
            Return False
        End If
    End Function
#End Region
#Region "ALTERAR"
    Function alterar() As Boolean
        Dim ret As Integer
        Try
            objDAL = New CaixaDAL
            ret = objDAL.alterar(Me.caicodigo, Me.caidescricao, Me.dataAbertura, Me.dataFechamento, Me.valorInicial, Me.valorCaixa, Me.Fechamento, Me.usuario, Me.status, Me.res)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing

        End Try
        If (ret = -1) Then
            Return False
        Else
            Return True
        End If
    End Function
#End Region
#Region "Soma"
    Function SomaSuplemento() As Decimal
        Dim ret As Decimal
        Try
            objDAL = New CaixaDAL
            ret = objDAL.SomaSuplemento(Me.caicodigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function SomaSangria() As Decimal
        Dim ret As Decimal
        Try
            objDAL = New CaixaDAL
            ret = objDAL.SomaSangria(Me.caicodigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function

    Function SomaVendas() As Decimal
        Dim ret As Decimal
        Try
            objDAL = New CaixaDAL
            ret = objDAL.SomaVendas(Me.caicodigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "VALIDA ABERTURA CAIXA"
    ' Metodo de validação de abertura de caixa
    Function validaAbertura() As Boolean
        Dim tab As DataTable  'se encontrar a consulta retorna true senao false
        Try
            objDAL = New CaixaDAL
            tab = objDAL.validaAbertura()
            For Each linha As DataRow In tab.Rows
                linha("cai_codigo").ToString()
                Me.caicodigo = tab.Rows(0)("cai_codigo").ToString()
                Return True
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objDAL = Nothing
        End Try
        Return False
    End Function
#End Region
End Class
